home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Pascal Super Library
/
Pascal Super Library (CW International)(1997).bin
/
MATH
/
STAT
/
STAT.DOC
< prev
next >
Wrap
Text File
|
1990-08-08
|
41KB
|
1,347 lines
Libraries of Elementary
Statistical and Mathematical
Subprograms
for
Turbo Pascal Version 5.X
Version 1.0
by
Norton Associates
506 Post Oak Dr.
Baytown Tx. 77520
Compuserve 70441,1337
SUMMARY OF FEATURES
Version 1.0 of this software contains 30 elementary statistical and 19
mathematical subprograms. The MATH Unit was designed to add the
FORTRAN-77 intrinsic math functions, except for complex math, that are
not provided by Turbo Pascal Version 5.X. However, some additional
math functions have been added to the MATH Unit as appropriate.
STAT Unit
- Uniform and normal random number generators
- 4 different types mean
- Summary Statistics ( first four moments, etc )
- Percentiles and quartiles
- Standard errors of parameters for normal distributions
- Cumulative frequency normal distribution statistics
- Integral frequency normal distribution statistics
- Correlation ( product moment and auto lag)
- Regression ( polynomial and multiple variable )
- Smoothing ( binomial, moving average and curvilinear )
- Sorting ( insert and quick )
- Other tools ( determinant and remove average )
MATH Unit
- Radian to degree conversion
- Normal trigonometric functions ( tan, secant etc )
- Inverse trigonometric functions ( atan2, arcsin etc )
- Hyperbolic functions ( sinh, cosh, tanh )
- Logarithmic functions ( power, log to any base etc )
- Other functions (factorial and root of an equation )
POSSIBLE FUTURE ENHANCEMENTS
- Cross lag correlation ( two variable)
- Increase variety of standard errors
- Contour plotting program
- Spline smoothing functions
- Integration
- Error functions
- Interpolation and extrapolation
- Gamma and Bessel functions
- Graphics ( plot function )
- Better use of 8087 coprocessor ( i.e., use 8087 tan
function
- Arrays larger than 16380 single precision elements. Have
created a single dimension array containing 130000
elements not 16380 as currently implemented.
- Complex Math functions
1.0 Introduction
1.1 Background
The statistical programs contained in the STAT Unit were originally
developed in FORTRAN over the years. With the advent of a good Pascal
environment, the use and need of FORTRAN to statistically crunch data
have diminished drastically.
The MATH Unit was developed because the language of Pascal does not
contain all the intrinsic math functions as those contained in the
standard FORTRAN-77 intrinsic function library [6]. The combination
of Turbo Pascal's math functions and those contained in this MATH Unit
provides the same functionality as those contained in the FORTRAN-77
intrinsic function library, except for complex math.
The user of these libraries are assumed to be familiar with
statistics. The definitions of terms used in these libraries can be
found by reviewing the reference contained in Section 5.0. Additional
statistical and mathematical tools will be added in the future.
This is a shareware product. Register your copy of the STAT and MATH
libraries by reading the README.DOC file contained with this
disk(ette) and sending in the nominal $35 to register the software.
1.2 Converting the Statistical Unit to Turbo Pascal
Several things happened while converting the statistical software from
FORTRAN to Pascal.
First, the size of the arrays - With the the advent of Personal
Computers (PC's), many mainframe FORTRAN programs have been converted
to PC's. In most cases special care had to be taken when the size of
the arrays multiplied by the size of the element exceeded 64k (65520
bytes). This limitation was due to the 8086 segmented architecture.
Most FORTRAN vendors implemented a way around the 8086 segmented
architecture. Well, Turbo Pascal has no provided such a capability
[1]. The segmented architecture of the 8086 family basically limits
the size of an array. The below table explains the size of the
element and the number of elements in the 65k 8086 architecture
boundary.
Table 1.
Element Size Maximum number of
type (bytes) elements in array STAT types
------- -------- ------------------ ----------------------
Single 4 16380 single_array_pointer
Longint 4 16380 longint_array_pointer
For example, this table states that the maximum number of elements
(indices of the array) of a single precision array is 16380 elements
in a 65K segment. This is satisfactory for most applications.
However, for some applications a vast increase in the number of
elements in the array was needed. To make a long story short, I have
developed a single precision array which which contains 130000
elements in the array. This implies that the memory requirements for
this array size alone is 520000 bytes. I have not completed testing
of this version of the software.
Second, the 640k DOS limitation - There are two major issues. How can
a user make best use of the 640k DOS limitation and if the user needed
more memory could he/she use extended/expanded memory for array
storage? The latter issue is not addressed in this version of the
software. The former issue was challenging enough.
With a fixed size of DOS, the number of arrays and the number of
elements in the arrays is very finite. Not knowing the number of
elements required by each user dictated that the STAT unit use the
maximum number of elements from Table 1. If the arrays were
statically created this meant that only 9 arrays of 65k bytes could
ever be created. This was totally not satisfactory. The solution was
to dynamically create the arrays on the heap. The user could then
have the best of both worlds. Basically, heap is the part of memory
between the top of the application in memory and the 640k DOS
limitation. By dynamically creating the arrays, the number of arrays
in the application is now very large and the size of each array may be
up to the full 65k limit. More information on this subject is
contained in the next section.
Finally, the precision - To make the STAT unit more precise, all
normally double precision numbers are in extended precision. By
adding two more bytes per variable, the number of significant digits
increased by four (15 to 19 digits).
1.3 User's Guide to the STAT and MATH Units
1.3.1 Format for the Libraries in Sections 3.0 and 4.0
A standard format for presenting the subprograms contained in the
libraries have been developed for Sections 3.0 and 4.0. An outline of
the standard format is presented directly below.
Name of subprogram here
Purpose: A short purpose statement goes here
Interface: The Turbo Pascal Unit Interface
Errors: Any errors are reported here
Remarks: Any special considerations go here
Example: Sample code go here
1.3.2 STAT Unit
To prevent any unforeseen problems, a series of STAT Unit procedures
have been developed to dynamically create and delete arrays. These
procedures are listed below.
Table 2.
Procedures for
Element Dynamic Creation and Deletion Procedures
Type Create Delete
--------- ------------------- -------------------
Single create_single_array delete_single_array
Longint create_longint_array delete_longint_array
The creation and deletion of arrays is shown in the demo called
DYNAMIC.PAS. The user should execute DYNAMIC.EXE. Basically, the
user must initiate and create the array before any information is put
into it. To initiate an array the user must know what type of an
array you want. For example, if the user wants a single dimensioned
array, he must initiate the array in the VAR section of the Pascal
code with one of the STAT types mentioned in Table 1. Once Initiated
the user may then create the array. This is done by selecting
corresponding creation procedures from Table 2. Once created, the
user may then use the array as any other array. Because the arrays
are created on the heap, the identification of the array is not
"lead[1]" but "lead^[1]". The caret (^) indicates to Pascal that the
array was created on the heap. The following example demonstrates the
creation process.
program demo;
const
max = 15000; { 15000 elements in the array }
var
lead : single_array_pointer; { Step 1: initiate a single type }
begin
create_single_array(max,lead); { Step 2: create the lead array of
max elements }
for j := 1 to max do { Step 3: use the array normally }
lead^[j] := j;
end.
The Pascal programs MULREG.PAS and TIMER.PAS are additional demo
programs to see the STAT Unit in action. The Pascal constants and
types for the MATH and STAT Units are defined in Section 2.0. All
subprograms of the STAT Unit are discussed using the format described
above are in Section 3.0.
1.3.3 MATH Unit
The MATH Unit functions are designed to perform just like the math
intrinsic function contained in FORTRAN-77. The Pascal program
MATHTEST.PAS is provided as a demo to test most MATH unit subprograms.
The MATH Unit constants are defined in Section 2.0 and a description
of these functions are contained in Section 4.0
2.0 Predefined Constants and Types
2.1 Predefined Library Constants
The following constants are defined in the STAT and MATH units. The
user may use these constants as needed in programs. Make sure that
you do not change these constant values. Be careful, Turbo Pascal
does allow the programmer to change the values defined in the constant
section.
STAT Unit
error = -99.9; { for most errors}
maxsize = 65520; { max segment size }
maxsingle = maxsize div (sizeof(single)); { 16380 }
maxlongint = maxsize div (sizeof(longint)); { 16380 }
v1 = 1; { constants }
v2 = 10000; { for uniform random}
v3 = 3000; { number generators}
maxorder = 10; { max order of polynomial}
maxmatrix = maxorder * 2 - 1; (19)
zero = 0.0;
one = 1.0;
two = 2.0;
uno = 1;
dos = 2;
c2 = 2.0;
c3 = -3.0;
c4 = 4.0;
c5 = 5.0;
c6 = 6.0;
c11 = 11.0;
c12 = 12.0;
i_4 = 1.0/c4; { 0.25 };
i_16 = 1.0/16.0;
i_35 = 1.0/35.0;
MATH Unit
pi = 3.14159265359;
pi_2 = pi / 2.0;
pi2 = pi * 2.0;
rad = pi / 180.0
i_rad = 180.0 / pi;
one = 1.0;
zero = 0.0;
infinity = 1.0E+09;
2.2 Predefined Types
STAT Unit
single_array_dummy = array[1..maxsingle] of single;
* single_array_pointer = ^single_array_dummy;
longint_array_dummy = array[1..maxlongint] of longint;
* longint_array_pointer = ^longint_array_dummy;
* quartype = array[1..5] of single;
* arry_type = array[1..maxorder,1..maxorder] of extended
------------
* Only use these in VAR section of Turbo Pascal program
MATH Unit
None
3.0 Statistical Unit
3.1 Random Number Generators
3.1.1 Uniform1
Purpose: Calculate a uniform distribution floating point number
between 0.0 and 1.0 [12].
Interface: function uniform1: single;
Errors: None
Remarks: v1,v2 and v3 are initial seeds that are defined as
constants and may be altered for different random number
sequences. The domain of these seeds are between 1 and
32767.
Example: begin
x := uniform1;
end;
3.1.2 Rndnormal1
Purpose: Generate a floating point number whose sample population is
normal and between +- 6 standard deviations
Interface: function rndnormal1(mean , standev : extended) : single;
Where: mean = mean of sample population and
standev = standard deviation of sample population
Errors: None
Remarks: Rndnormal1 is based on the polar coordinate normal random
number generator [11].
Example: begin
mean = 100.0;
sd = 5.0;
x := rndnormal1(mean,sd);
end;
3.1.3 Rndnormal2
Purpose: Provide an approximate normal distribution random floating
point number between +- 6 standard deviations
Interface: function rndnormal2(mean , standev : extended) : single;
Where: mean = mean of expected sample population
standev = standard deviation of expected sample
population
Errors: None
Remarks: This normal random number is an approximation to the normal
random discussed previously [5].
Example: begin
mean = 100.0;
sd = 5.0;
x := rndnormal2(mean,sd);
end;
3.2 Sorting
3.2.1 Insert Sort
Purpose: Classical data structure for sorting floating point
numbers.
Interface: procedure insert(n: word; var a: single_array_pointer);
Where: n = number of values to be sorted
a = array of values to be sorted
Errors: insert> no sample data - Number of sample data points less
than two.
Remarks: For arrays up to approximately 15 floating point numbers
the insert sort is more efficient than quicksort. The code
and data requirements of insert are smaller than quicksort.
The original order of the input array is destroyed.
Example: begin
insert(n,a);
end;
3.2.2 Quick sort
Purpose: Provided a very fast way for sorting floating point
numbers.
Interface: procedure qsort(n: word; var a: single_array_pointer);
Where: n = number of values to be sorted
a = array of values to be sorted
Errors: 1) quick> no sample data - Number of sample data point less
than 2.
2) quick> stack space exceeded - number of stacks used in
quicksort has been exceeded.
Remarks: Quick sort provides a very fast way of sorting floating
point numbers. The original order of the input array is
destroyed.
Example: begin
quick(n,a);
end;
3.3 Means
3.3.1 Means
Purpose: Calculate the arithmetic, geometric, harmonic and root mean
square means for the sample data [8].
Interface: procedure means(n: word; a : single_array_pointer;
var xmean, gmean, hmean, rmsmean : single);
Where: xmean = arithmetic mean
gmean = geometric mean
hmean = harmonic mean
rmsmean = root mean square mean
Errors: 1) means> no sample data: n < 2 - Less than two data points
provided.
2) means> Gmean can not be calculated - There is a zero or
negative number in the sample data.
3) means> Hmean can not be calculated - There is a zero or
negative number in the sample data
Remarks: If there are no variations in the data then all means have
the same value. As the variation in the data increases,
the difference between the means increase. Each type of
mean have their own advantages and disadvantages.
Example: begin
means(n,a,xmean,gmean,hmean,rmsmean);
end;
3.4 Summary statistics
3.4.1 Elementary statistics
Purpose: Calculate elementary statistics of data
Interface: elem_stat(n : word; a : single_pointer_array;
var small, large, mean, sd : single);
Where: small = smallest value
large = largest value
mean = arithmetic mean
sd = adjusted standard deviation for sample
population (i.e., n - 1).
Errors: 1) elem_stat> no sample data - 0 or negative number of data
points.
2) elem_stat> number of sample = 1: no standard deviation -
standard deviation can not be calculated with one data
point.
Remarks: Elem_stat is a very simple way of calculating the simple
statistics of the sample population. Other statistical
parameters may be calculated from this data (i.e., range =
large - small + 1).
Example: begin
elem_stat(n,a,small,large,mean,sd);
end;
3.4.2 Weighted arithmetic mean
Purpose: Calculate a frequency or weighted summary statistics
Interface: wxmean(n : word; a : single_array_pointer; freq :
longint_array_pointer; var small, large, mean, sd :
single);
Where: freq = interval count of data for each corresponding
value in the a array
Errors: 1) wxmean> no sample data point - number of data points
less than one.
2) wxmean> no standard deviation calculated - Standard
deviation can not be calculated from one data point.
Remarks: Do not forget that freq contains the frequencies for each
sample value in the a array, respectively.
Example: begin
wxmean(n,a,freq,small,large,mean,sd);
end;
3.4.3 Moments
Purpose: Calculate the first 4 moments for a sample population
Interface: procedure moments( n:word; a : single_array_pointer;
var mean,sd,skew,beta2: single);
Where: skew = skewness
beta2 = beta2.
Errors: 1) moments> no sample data - number of sample data point
less than 2.
2) moments> no data for beta2 and skewness - the standard
deviation was zero.
Remarks: This procedure provides a simple way of calculating the
first through the fourth moments. Qurtosis = beta2 - 3;
Example: var a : single_array_pointer;
begin
create_single_array(n,a);
...put data into array a...;
moments(n,a,mean,sd,skew,beta2);
end;
3.5 Percentiles
3.5.1 Quartiles
Purpose: Calculate the first through third quartile and smallest and
largest value of the sample population
Interface: procedure quartile( n: word; a : single_array_pointer;
var quart : quartype );
Where quart is an array of 5 elements containing the 3
quartiles and the smallest and largest.
Errors: quartile> no sample data - number of sample data point less
than 2.
Remarks: The smallest = quart[1] 0.0 percentile
first quartile = quart[2] 25.0 percentile
second quartile = quart[3] 50.0 percentile
third quartile = quart[4] 75.0 percentile
largest = quart[5] 100.0 percentile
Example: var quart: quartype:
begin
quartile(n,a,quart);
end;
3.5.2 Percentile
Purpose: Calculate a percentile of the sample population
Interface: function percentile( n : word; a : single_array_pointer;
percent : single ): single;
Errors: percentile> no sample data - Number of sample data points
less than 2.
Remarks: Make sure the variable percent is a percentile (e.g., %)
Example: begin
x := percentile(n,a,23.0);
end;
3.6 Standard error
Purpose: Calculate the standard error [8].
Interface: function standard_error( n: word, sd : single ;
ntype : word) : single;
Where ntype standard error of
----- -----------------------
1 mean
2 sum
3 median
4 standard deviation
5 mean deviation
6 coefficient of variation
7 coefficient of correlation
Error: 1) standard_error> type out of range - ntype must be 1 - 7
inclusive
2) standard_error> no sample data - number of samples less
than 2
Remarks: These standard errors are based on a normal distribution.
Example: begin
x := standard_error(n,sd,1); { for the mean }
end;
3.7 Cumulative Distribution function: Normal distribution
3.7.1 From probability to standard deviation
Purpose: Calculate the correct standard deviation from a given
probability [14]
Interface: function cdf_prob_to_sd(prob: single): single;
Errors: cdf_prob_to_sd> input probability out of range - input
probability must between 0 and 1.
Remarks: The algorithm used is accurate to 4 decimal places
Example: begin
x := cdf_prob_to_sd(0.50); { x = 0.0 }
end;
3.7.2 From standard deviation to probability
Purpose: Calculate the correct probability from a given standard
deviation [14]
Interface: function cdf_sd_to_prob(sd: single): single;
Errors: None
Remarks: The algorithm used is accurate to 4 decimal places
Example: begin
x := cdf_sd_to_prob(0.0); { x = 0.50 }
end;
3.8 Integral Distribution function: Normal distribution
3.8.1 From probability to standard deviation
Purpose: Calculate the correct standard deviation from a given
probability [14]
Interface: function int_prob_to_sd(prob: single): single;
Errors: int_prob_to_sd> input probability out of range -
probability must be between 0 and 1.
Remarks: The algorithm used is accurate to 4 decimal places
Example: begin
x := int_prob_to_sd(0.66); { x = 1.0 }
end;
3.8.2 From standard deviation to probability
Purpose: Calculate the correct probability from a given standard
deviation [14]
Interface: function int_sd_to_prob(sd: single): single;
Errors: None
Remarks: The algorithm used is accurate to 4 decimal places
Example: begin
x := int_sd_to_prob(-1.0); { x = 0.37 }
end;
3.9 Correlation
3.9.1 Linear correlation coefficient
Purpose: Calculate the linear correlation coefficients via the
product moment technique
Interface: procedure corr_coef(n: word; x, y : single_array_pointer;
var r : single);
Where: x and y are the independent and dependent arrays,
respectively.
r = correlation coefficient
Errors: corr_coef> no sample data - number of data point less than
2.
Remarks: The correlation coefficient is adjusted for sample size
(e.g., n - 1)
Example: begin
... get x and y data...;
corr_coef(n,x,y,r);
end;
3.9.2 Linear Auto lag correlation coefficient
Purpose: Calculate the linear correlation coefficient via the
product moment technique for each step of the lag.
Interface: procedure auto_corr(n: word; x : single_array_pointer;
lag: word; var auto : single_array_pointer);
Where: lag = number of lags to calculate the auto
correlation coefficients
auto = an array of lag number of elements
Errors: 1) auto_corr> no sample data - number of sample data points
less than 2.
2) auto_corr> lag can not exceed number in sample data - no
resultant data points can be used.
Remarks: The correlation coefficient is adjusted for sample size
(e.g., n - 1)
Example: var x,auto : single_array_pointer
begin
... get x and y data...;
auto_corr(n,x,20,auto);
end;
3.10 Regression : least squares
3.10.1 Linear fit
Purpose: Calculate the linear regression coefficients and
correlation coefficient for pairs of x and y values [9].
Interface: procedure linfit(x,y,sigmay : single_array_pointer ;
n,mode: word; var a,b,r : single);
Where: sigmay = standard deviation of measurement for each
y variable
if mode = 0 then sigmay array not used
if mode = 1 then sigmay array is used
a = intercept
b = slope
r = linear correlation coefficient
y estimated = a + bx
Errors: linfit> number of data points < 2 - number of data points
used in linfit is less than 2
Remarks: Sigmay is not used in linfit if mode = 0
Example: var x,y,sigmay : single_array_pointer;
begin
... create x and y arrays...;
... put data into x and y arrays...;
linfit(x,y,sigmay,n,0,a,b,r);
end;
3.10.2 Polynomial fit
Purpose: Calculate the polynomial regression coefficients and
multiple regression correlation coefficient for pairs of x
and y values [9].
Interface: procedure polfit(n: word ;x, y, sigmay :
single_array_pointer ; nterms, mode, : word; var a:
single_array_pointer; r,se : single);
Where: sigmay is the same as in linfit
nterms = order + 1. For parabola nterms = 3
a = array of regression coefficients
r = multiple regression correlation coefficient
se = standard error of regression
y estimated = a[1] + a[2]*x etc
Errors: 1) polfit> number of data point < 2 - number of data points
used in polfit is less than 2.
2) polfit> maxorder of polfit is 10 - nterms was larger
than 10.
3) polfit> number of terms exceeds the number of data
points. The equations used in polfit required that the
number of data points exceed the number of data points.
4) polfit> determinant or correlation coefficient
approximately 0 - Polfit became unstable and reported the
error.
Remarks: Sigmay is never used in polfit if mode = 0
Example: var x,y,a : single_array_pointer;
begin
... create x and y arrays...;
... put data into x and y arrays...;
polfit(n,x,y,sigmay,3,0,a,r,se); ( parabola)
end;
3.10.3 Multiple Regression fit
Purpose: Calculate the multiple regression coefficients and multiple
regression correlation coefficient for pairs of x, y and z
values [7].
Interface: procedure mul_reg(n : word; x, z ,y : single_array_pointer
; var a: single_array_pointer; r,se : single);
Where: z = one of two arrays of independent measurements
a = array of regression coefficients
r = multiple regression correlation coefficient
se = standard error of regression
y estimated = a[1] + a[2]*x + a[3]*z
Errors: 1) mul_reg> sample data less than 4 - because this uses 3
constraints then number of data points required is 4.
2) mul_reg> determinant or correlation coefficient
approximately 0 - Mul_reg became unstable and reported the
error.
Remarks: Mul_reg is used for two independent and one dependent
variable arrays.
Example: var x,z,y,a : single_array_pointer;
begin
... create x and y arrays...;
... put data into x and y arrays...;
mul_reg(n,x,z,y,a,r,se);
end;
3.11 Data Smoothing
3.11.1 Binomial 1-2-1
Purpose: Smooth data with a binomial 1-2-1 function [9]
Interface: procedure smooth121(n:word; var a: single_array_pointer);
Errors: smooth121> no sample data - number of data points less than
3.
Remarks: This procedure smooths out aberrations in the data
Example: begin
smooth121(n,a);
end;
3.11.2 Binomial 1-4-6-4-1
Purpose: Smooth data with a binomial 1-4-6-4-1 function [9]
Interface: procedure smooth14641(n:word; var a: single_array_pointer);
Errors: smooth14641> no sample data - number of data points less
than 5.
Remarks: This procedure has approximately twice the smoothing power
of smooth121.
Example: begin
smooth14641(n,a);
end;
3.11.3 Curvilinear
Purpose: Smooth data which is markedly concave or convex [13]
Interface: procedure smoothcurve(n:word; var a: single_array_pointer);
Errors: smoothcurve> no sample data - number of data points less
than 6.
Remarks: This procedure smooths data points that are from a very
concave or convex function.
Example: begin
smoothcurve(n,a);
end;
3.11.4 Moving Average
Purpose: Smooth data using moving average approach
Interface: function mov_avg(n:word; var a: single_array_pointer; ma,
counter: word);
Where: ma = number of elements in moving average.
counter = current pointer in array
Errors: movavg> number of data points less than moving average -
The starting point of the calculation is less than the
first data point.
Remarks: None
Example: begin
x := mov_avg(n,a,ma,j);
end;
3.12 Other tools
3.12.1 Determinant
Purpose: Calculate the determinant [3]
Interface: function determ(arry : arry_type ; norder : integer) :
extended
Where: arry = array of values to calculate the determinate.
norder = order of the arry
Errors: determinate> Determinate contains a zero - Can not solve
equation with a zero.
Remarks: None
Example: const norder = 3;
var arry : arry_type;
begin
stuff data into arry
x := determ(arry,norder);
end;
3.12.2 Remove Average
Purpose: Remove the average or any constant from the values in the
data array
Interface: function remove_avg(n:word; var a: single_array_pointer;
avg: single);
Where: avg = value to be deleted from the array.
Errors: Remove_avg> number of data points less than 2 - There to
few data points.
Remarks: None
Example: begin
x := remove_avg(n,a,avg);
end;
4.0 Mathematical
4.1 Radian Conversion
4.1.1 Degrees to Radians
Purpose: To convert from degrees to radian
Interface: function deg_rad( x : single) : single);
Errors: None
Remarks: None
Example: begin
y := deg_rad(180.0); (y = 3.14 radians)
end;
4.1.2 Radians to Degrees
Purpose: To convert from radians to degrees
Interface: function rad_deg( x : single) : single);
Errors: None
Remarks: None
Example: begin
y := rad_deg(pi); (y = 180.0 degrees)
end;
4.2 Trigonometric Functions
4.2.1 Tan
Purpose: Turbo Pascal does not have a Tan
Interface: function tan( x : single) : single;
Errors: Cosine term can not be zero
Remarks: None
Example: begin
x = tan(deg_rad(45.0)); ( x = 1.0)
end;
4.2.2 Secant
Purpose: Turbo Pascal does not have a Secant
Interface: function secant( x : single) : single;
Errors: Cosine term can not be zero
Remarks: None
Example: begin
x = secant(deg_rad(45.0)); ( x = 1.414)
end;
4.2.3 Cosecant
Purpose: Turbo Pascal does not have a Cosecant
Interface: function cosecant( x : single) : single;
Errors: Sine term can not be zero
Remarks: None
Example: begin
x = cosecant(deg_rad(45.0)); ( x = 1.414)
end;
4.2.4 Cotan
Purpose: Turbo Pascal does not have a Cotan
Interface: function cotan( x : single) : single;
Errors: Tan term can not be zero
Remarks: None
Example: begin
x = cotan(deg_rad(45.0)); ( x = 1.0)
end;
4.3 Inverse Trigonometric Functions
4.3.1 Arc sin
Purpose: Turbo Pascal does not have a arcsin
Interface: function arcsin( x : single) : single;
Errors: Input value x must be within plus and minus 1.0
Remarks: Uses arctan identity to solve for arcsin
Example: begin
x = rad_deg(arcsin(sin(deg_rad(45.0)))); ( x = 45.0)
end;
4.3.2 Arc cosine
Purpose: Turbo Pascal does not have a arc cosine
Interface: function arccos( x : single) : single;
Errors: Input value x must be within plus and minus 1.0
Remarks: Uses arctan identity to solve for arccos
Example: begin
x = rad_deg(arccos(cos(deg_rad(45.0)))); ( x = 45.0)
end;
4.3.3 Arc Tangent : four quadrants
Purpose: Turbo Pascal does not have a arc tangent for all four
quadrants
Interface: function arctan2( x,y : single) : single;
Errors: x and y values must not be zero
Remarks: Uses Turbo Pascal atan to initially solve the arctan2 and
then the signs of the x and y terms dictate the quadrant
Example: begin
x := rad_deg(arctan2(tan(deg_rad(45.0),1.0))); ( x = 45.0)
end;
4.4 Hyperbolic Functions
4.4.1 Hyperbolic sine
Purpose: Turbo Pascal does not have a sinh function
Interface: function sinh( x : single) : single;
Errors: None
Remarks: None
Example: begin
x = sinh(5.0) (x = 74.210)
end;
4.4.2 Hyperbolic Cosine
Purpose: Turbo Pascal does not have a cosh function
Interface: function cosh( x : single) : single;
Errors: None
Remarks: None
Example: begin
x = cosh(5.0) (x = 74.203)
end;
4.4.3 Hyperbolic Tangent
Purpose: Turbo Pascal does not have a tanh function
Interface: function tanh( x : single) : single;
Errors: None
Remarks: None
Example: begin
x = tanh(5.0) (x = 1.0)
end;
4.5 Logarithmic Functions
4.5.1 Power
Purpose: To raise a real number to a real number (e.g., x**y)
Interface: function power(x,y: extended) : extended;
Where x = number to be raised
y = raised value
Errors: None
Remarks: Uses standard logarithmic functions to perform function.
Extended precision is used to increase precision of
resulting number and increase the range of answer.
Example: begin
z := power(2.0,3.0) ( z = 8.0)
end;
4.5.2 Logarithms to base 10
Purpose: Turbo Pascal does not provide a logarithms to base 10
Interface: function log10(x : single) : single;
Errors: None
Remarks: Uses standard logarithmic functions to perform function
Example: begin
z := log10(1000) ( z = 3.0)
end;
4.5.3 Logarithms to any base
Purpose: Turbo Pascal does not provide a logarithms to any base
Interface: function logxy(x,y : single) : single;
Where y = base
x = base 10 number to be converted to base y
Errors: ln(y) must not be zero
Remarks: Uses standard logarithmic functions to perform function
Example: begin
z := logxy(1000.0,2.0) ( z = 9.966)
end;
4.6 Other Useful Mathematical Functions
4.6.1 Double Precision Product
Purpose: It is very difficult in Turbo Pascal to guarantee that two
single precision number can be correctly be multiplied to
yield a double precision answer
Interface: function dprod( x,y: extended) : extended;
Errors: None
Remarks: The two single precision numbers may be actual parameters
of the function call. The formal arguments are converted
to extended precision, multiplied in extended precision and
then returned in extended precision.
Example: var x,y : single;
z : extended;
begin
z := dprod(1/3,1.0); (z = 0.333333333333333333)
(not z =0.333333343 etc)
end;
4.6.2 Convert to Double Precision
Purpose: It is very difficult in Turbo Pascal to convert a single
precision number to a double precision number.
Interface: function dble( x extended) : extended;
Errors: None
Remarks: Not that there is no gain in precision of the original
number.
Example: var x : single;
z : extended;
begin
z := dble(1/3);
end;
4.6.3 Root of an Equation : secant method
Purpose: Determine the root of an equation using secant method [3]
Interface: procedure secantmethod( xn, xn_1, fn, fn_1 : extended) :
extended;
Where xn = x(n)
nn_1 = x(n-1)
fn = f(n)
fn_1 = f(n_1)
Errors: None
Remarks: None
Example:var
xn,xn_1,fxn,fxn_1 : extended;
guess : integer;
function formula (xn: extended) : extended;
const
c2 : extended = 2.0;
c3 : extended = 3.0;
begin
formula := power(xn,c3) - c2*power(xn,c2) + xn - c3;
end;
begin
xn := 3.0;
xn_1 := 4.0;
repeat
fxn := formula(xn);
fxn_1 := formula(xn_1);
secantmethod(xn,xn_1,fxn,fxn_1);
until abs(xn - xn_1) <= 1.0E-17;
converges to 2.17455941029297 etc.
end.
4.6.4 Factorial
Purpose: Provide a n! function
Interface: function factorial( n: word) : single;
Errors: None
Remarks: None
Example: begin
z := factorial(5); (z = 120)
end;
5.0 References
[1] Mastering Turbo Pascal Version 5.5, Third Edition, Tom Swan,
Hayden Books, 1990
[2] Pascal Programs : For Scientists and Engineers, Alan Miller,
Sybex, 1981
[3] Numerical Mathematics and Computing, Ward Cheney and David
Kincaid, Brooks/Cole Publishing, 1980
[4] SPSS/PC+ User's Manual, SPSS Inc., 1986
[5] Digital Computer Methods in Engineering, S. Hovanessian and
L.Pipes, McGraw-Hill, 1969
[6] Programming in Standard FORTRAN 77, A. Balfour and D. Marwick,
North-Holland, 1979
[7] Introduction to Statistical Analysis,
[8] Statistical Methods, H. Arkin and R. Colton, Barnes & Noble, 1970
[9] Data Reduction and Error Analysis for the Physical Sciences, P.
Bevington, McGraw-Hill, 1969
[10] Calculus with Analytical Geometry, E. Purcell,
Appleton-Century-Crofts, 1965
[11] Knuth, Volume 2
[12] BYTE Magazine, March 1987
[13] Handbook of Statistical Methods in Meteorology,
[14] Handbook of Mathematical Functions, Ambrowitz, 1982